fbpx

Using the Azure CLI to deploy Azure Bicep templates can be used to more efficiently build Infrastructure as Code (IaC) deployment workflows for managing Microsoft Azure resources. Azure Bicep templates and the Azure CLI are both powerful tools for any DevOps Engineer or Site Reliability Engineer (SRE) to add to their toolbox. In this article, we will explain how to deploy an Azure Bicep template using the Azure CLI, step-by-step, and provide code examples to help guide you through the process.

Prerequisites

Before we begin, make sure that you have the following tools and resources installed and ready to go:

Related: If you’re just getting started with writing Infrastructure as Code (IaC) automation of Microsoft Azure resources using either Azure Bicep or the Azure CLI, please read the following articles:

Step 1: Create a Bicep template

The first step in deploying an Azure Bicep template is to create the Bicep file that defines your resources. You can create a new Bicep file using a text editor or an integrated development environment (IDE) that supports Bicep.

Here is an example of a Bicep file that creates a virtual network with a single subnet:

param location string
resource vnet 'Microsoft.Network/virtualNetworks@2020-11-01' = {
  name: 'myVNet'
  location: location
  properties: {
    addressSpace: {
      addressPrefixes: [
        '10.0.0.0/16'
      ]
    }
    subnets: [
      {
        name: 'mySubnet'
        properties: {
          addressPrefix: '10.0.0.0/24'
        }
      }
    ]
  }
}

Step 2: Sign in to your Azure Subscription

Before you can deploy the Bicep template, you need to sign in to your Azure account using the Azure CLI. To sign in, run the following command:

az login

This will open a browser window where you can enter your Azure account credentials and authenticate. Alternatively, you can use an Azure AD Service Principal to authenticate with the Azure CLI. This is useful when automating Azure CLI scripting and Azure Bicep template deployment using a CI/CD tool like Azure DevOps or GitHub Actions.

Step 3: Create an Azure Resource Group

To deploy the Bicep template, you need to have a resource group created in your Azure subscription. If you don’t already have a resource group, you can create one using the following command:

az group create --name <resource-group-name> --location <location>

Replace <resource-group-name> with the name of your resource group, and <location> with the location where you want to create the resource group.

Step 4: Deploy the Azure Bicep Template

Now that you have your Bicep template and resource group ready, you can deploy the template using the Azure CLI. To deploy the template, run the following command:

az deployment group create --resource-group <resource-group-name> --template-file <filename>.bicep

Replace <resource-group-name> with the name of your resource group, and <filename>.bicep with the name of your Azure Bicep template file.

Optional: Build the Bicep file into ARM Template

This Azure Bicep build step to an ARM Template is completely optional, and can be used as an intermediate step before the Azure Bicep template deployment. This will build, or transpile, the Azure Bicep code into an Azure ARM Template. The resulting ARM Template can then be deployed to Azure using the same Azure CLI az deployment group create command.

After creating the Bicep file, the next step is to build it using the Bicep compiler. The Bicep compiler compiles the Bicep file into an ARM template that can be deployed to Azure.

To build the Bicep file, navigate to the directory where the Bicep file is saved, and run the following command:

bicep build <filename>.bicep

This will create an ARM Template file with the same name as the Azure Bicep file, but with a .json extension.

Transpile: To transpile is to use a program or tool that translates source code from one programming language to another, using the same level of abstraction. Generally, both languages will be human readable, and the transpiler essentially converts from the source language to the destination language.

Conclusion

This article covered how to deploy an Azure Bicep template using the Azure CLI to manage Microsoft Azure cloud resources. By following these steps, you can create, build, and deploy Azure Bicep templates in an automated fashion when using these tools to setup Infrastructure as Code (IaC) management of Microsoft Azure resources.

Microsoft MVP

Chris Pietschmann is a Microsoft MVP, HashiCorp Ambassador, and Microsoft Certified Trainer (MCT) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to large enterprises. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
HashiCorp Ambassador Microsoft Certified Trainer (MCT) Microsoft Certified: Azure Solutions Architect

Discover more from Build5Nines

Subscribe now to keep reading and get access to the full archive.

Continue reading